# 文件操作 文件读写操作
#     'r'       open for reading (default)
#     'w'       open for writing, truncating the file first
#     'x'       create a new file and open it for writing
#     'a'       open for writing, appending to the end of the file if it exists
#     'b'       binary mode
#     't'       text mode (default)
#     '+'       open a disk file for updating (reading and writing)
#     'U'       universal newline mode (deprecated)

# 第二个参数 读取字节数
# encode decode bin/str 编码/解码操作


# read
import os
import random

file_reader = open("D:/练习题.txt", "r", encoding="UTF-8", closefd=True)
# print(file_reader.readlines())
file_reader.close()
# write
file_writer = open("D:/test.txt", "a", encoding="utf-8", closefd=True)
file_writer.write("测试数据")
file_writer.close()

# 二进制读写
file_writer_b = open("D:/test.txt", "rb", closefd=True)

# os 修改文件名称
# os.rename("", "")
# os.listdir("D:/")
# os.mkdir("D:/abc/")
# print(os.getcwd())

# 创建临时文件
# os.chdir("D:/abc")
# for item in range(50):
#     open(f"D:/abc/{item}.txt", "a", encoding="utf-8", closefd=True)

# 批量修改文件夹 切换路径: os.chdir("D:/abc")
os.chdir("D:/abc")
file_list = os.listdir()
for item in file_list:
    index = item.index(".")
    new_file_name = item[:index].replace("x", "")
    os.rename(item, f"{new_file_name}.txt")
